const {
  courseFormatError,
  saveCourseInfoError
} = require('../constant/err.type')
const validator = async (ctx, next) => {
  try {
    ctx.verifyParams({
      course_name: { type: 'string', required: true },
      course_price: { type: 'number', required: true },
      course_teacher: { type: 'string', required: true },
      course_img: { type: 'string', required: true }
    })
  } catch (err) {
    console.log(err)
    courseFormatError.result = err.errors
    return ctx.app.emit('error', courseFormatError, ctx)
  }

  await next()
}

//保存课程信息验证
const saveCourseInfoValitator = async (ctx, next) => {
  try {
    ctx.verifyParams({
      title: { type: 'string', required: true },
      teacher_id: { type: 'string', required: true },
      subject_id: { type: 'string', required: true },
      subject_parent_id: { type: 'string' },
      price: { type: 'number', required: true },
      lesson_num: { type: 'number', required: true },
      buy_count: { type: 'number' },
      view_count: { type: 'number', required: true },
      version: { type: 'number', required: true },
      status: { type: 'string' },
      is_deleted: { type: 'number', required: true }
    })
  } catch (err) {
    console.log(err)
    saveCourseInfoError.result = err.errors
    return ctx.app.emit('error', saveCourseInfoError, ctx)
  }

  await next()
}

module.exports = {
  validator,
  saveCourseInfoValitator
}
